﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using Wicresoft.SelfService.DAO;
using Wicresoft.EKA.AccountInfoUpdateApp.Init;
using Wicresoft.EKA.AccountInfoUpdateApp.Service;
using Wicresoft.EKA.AccountInfoUpdateApp.Model;

namespace Wicresoft.EKA.AccountInfoUpdateApp
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine(" To update user info changed to database! \n Please click Enter key!");
            Console.ReadLine();
            Console.WriteLine(" Starting...\n");

            string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = "+Environment.CurrentDirectory+@"\\DataPool\\Data.xls;Extended Properties=Excel 8.0 ";

            OleDbConnection myConn = new OleDbConnection(strCon);
            myConn.Open();
            string[] Sheet = new string[] { "UserInfo", "LoginIDIsNull" };
            //string[] Sheet = new string[] { "LoginIDIsNull" };
            //string[] Sheet = new string[] { "UserInfo" };
            for (int n = 0; n < Sheet.Length; n++)
            {
                DataSet myDataSet = new DataSet();
                string StrSql = "select * from [" + Sheet[n] + "$]";
                OleDbDataAdapter myCommand = new OleDbDataAdapter(StrSql, myConn);
                myCommand.Fill(myDataSet, "[" + Sheet[n] + "$]");
                myCommand.Dispose();
                DataTable DT = myDataSet.Tables["[" + Sheet[n] + "$]"];
                myConn.Close();
                myCommand.Dispose();

                #region  //Update
                if (Sheet[n].ToString() == "UserInfo")
                {
                    AccountInfo model = new AccountInfo();
                    string r0 = "";
                    string r1 = "";
                    string r2 = "";
                    string r3 = "";
                    string r4 = "";
                    string r5 = "";

                    bool ret = false;
                    int status = 0;
                    for (int j = 1; j < DT.Rows.Count; j++)
                    {
                        int row = j + 2;
                        Console.WriteLine("Check the Excel data row :" + row);

                        r0 = DT.Rows[j][0].ToString().Replace(" ", "");
                        r1 = DT.Rows[j][1].ToString().Replace(" ", "");
                        r2 = DT.Rows[j][2].ToString().Replace(" ", "");
                        r3 = DT.Rows[j][3].ToString().Replace(" ", "");
                        r4 = DT.Rows[j][4].ToString().Replace(" ", "");
                        r5 = DT.Rows[j][5].ToString().Replace(" ", "");

                        if (string.IsNullOrEmpty(r0))
                        {
                            break;
                        }
                        else
                        {
                            model = UpdateInit.GetAllAccountInfo(r0);

                            if (model != null)
                            {
                                //if (model.Company != r1)
                                //{
                                //    model.Company = r1;
                                //    status = 1;
                                //}
                                if (model.Dept != r2)
                                {
                                    model.Dept = r2;
                                    status = 1;
                                }
                                if (model.Position != r3)
                                {
                                    model.Position = r3;
                                    status = 1;
                                }
                                if (!string.IsNullOrEmpty(r4))
                                {
                                    if (string.IsNullOrEmpty(model.Phone))
                                    {
                                        model.Phone = r4;
                                        status = 1;
                                    }
                                    else if (model.Phone != r4)
                                    {
                                        model.Phone = r4;
                                        status = 1;
                                    }
                                }
                                else
                                {
                                    if (string.IsNullOrEmpty(model.Phone))
                                    {
                                        model.Phone = "1234567";
                                        status = 1;
                                    }
                                }

                                if (!string.IsNullOrEmpty(r5))
                                {
                                    if (model.Mail != r5)
                                    {
                                        model.Mail = r5;
                                        status = 1;
                                    }
                                }

                                if (status == 1)
                                {
                                    ret = UpdateInit.ChangeUserInfo(model);
                                    string msg = "[" + row + "]" + r0.ToString();
                                    if (!ret)
                                    {
                                        SysLog.WriteSysLog(msg);
                                        SysLog.WriteSysLog("-------------------------------------------------------------------------------");
                                    }
                                    Console.WriteLine(msg);
                                }
                            }
                        }

                    }
                }
                #endregion

                #region  //Create
                if (Sheet[n].ToString() == "LoginIDIsNull")
                {
                    User_XModel model = new User_XModel();
                    CreateInit createInit = new CreateInit();
                    int ret = 0;

                    string r0 = "";
                    string r1 = "";
                    string r2 = "";
                    string r3 = "";
                    string r4 = "";
                    string r5 = "";
                    string r6 = "";

                    for (int j = 1; j < DT.Rows.Count; j++)
                    {
                        int row = j + 2;
                        Console.WriteLine("Check the Excel data row :" + row);
                        r0 = DT.Rows[j][0].ToString().Replace(" ", "");
                        r1 = DT.Rows[j][1].ToString().Replace(" ", "");
                        r2 = DT.Rows[j][2].ToString().Replace(" ", "");
                        r3 = DT.Rows[j][3].ToString().Replace(" ", "");
                        r4 = DT.Rows[j][4].ToString().Replace(" ", "");
                        r5 = DT.Rows[j][5].ToString().Replace(" ", "");
                        r6 = DT.Rows[j][6].ToString().Replace(" ", "");


                        if (string.IsNullOrEmpty(r0))
                        {
                            break;
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(r4))
                                r4 = "职员";
                            if (string.IsNullOrEmpty(r5))
                                r5 = "1234567";
                            if (string.IsNullOrEmpty(r6))
                                r6 = r0 + DataHelp.EmailInit(r2);

                            model.Name = r0;
                            model.DisplayName = r1;
                            model.Company = r2;
                            model.Department = r3;
                            model.Position = r4;
                            model.Mobile = r5;
                            model.Email = r6;
                            model.Password = r0;
                            ret = createInit.Create(model);

                            if (ret == 1)
                            {
                                string msg = "Exist : " + r0 + " ; Row [" + row + "]";
                                SysLog.WriteSysLog(msg);
                                SysLog.WriteSysLog("-------------------------------------------------------------------------------");
                            }
                            else if (ret == 2)
                            {
                                string msg = "Error : " + r0 + " ; in row [" + row + "]";
                                SysLog.WriteSysLog(msg);
                                SysLog.WriteSysLog("-------------------------------------------------------------------------------");
                            }
                        }
                    }
                #endregion

                }

                Console.WriteLine(" All user info data changed were updated!");
                Console.ReadLine();

            }
        }
    }
}
